package com.example.mypopupwindow;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends AppCompatActivity {

    private static final String TAG ="leo";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void leoClick(View view) {
        View popupView=getLayoutInflater().inflate(R.layout.popupwindow_view,null);//获取view文件

        Button btn1= popupView.findViewById(R.id.btn1);
        Button btn2= popupView.findViewById(R.id.btn2);

        PopupWindow popupWindow=new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
                ,true);//ViewGroup.LayoutParams.WRAP_CONTENT相当于在xml中配置长宽,最后一个参数表示可以在空白出点击退出
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.test_2));//设置背景色
        popupWindow.showAsDropDown(view,0,0); //最后两个参数x。y用来指定view的位置，不设置默认显示在下方
                                        //view.getWidth(),view.getHeight()
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG,"你是住在上海吗");
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG,"你是住在北京吗");
                popupWindow.dismiss(); //退出操作
            }
        });

    }
}